<?php
namespace App\Models\Traits;
use App\Logics\AdminOperationLogLogic;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Log;

trait ModelLog {
    public function boot_log($trans, $class, $title, $is_create = false)
    {
		$changes=[];
        try {
			$fields = $class::$edit_fields;
			foreach($fields as $field) {
                if ($is_create) {
                    // 新增
                    if (!$this->$field) {
                        continue;
                    }
                    $current_value = $class::getTextValueForLog($field, $this->$field);
                    $changes[] = (isset($trans[$field]) ? $trans[$field] : $field) . '：' . $current_value;
                } else {
                    // 修改
                    if ($this->isDirty($field)) {
                        $origin_value = $class::getTextValueForLog($field, $this->getOriginal($field));
                        $current_value = $class::getTextValueForLog($field, $this->$field);
                        $changes[] = (isset($trans[$field]) ? $trans[$field] : $field) . '：' . $origin_value . ' 修改为 ' . $current_value;
                    }
                }
			}
			if ($changes) {
                $student = $this->student;
                array_unshift($changes, $title);
				AdminOperationLogLogic::make($student)->update($changes)->log();
			}
		} catch (\Throwable $th) {
			Log::info($title . $th->getMessage());
		}
    }
}